package com.egg.ylt.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.egg.ylt.Utils.Constants;
import com.egg.ylt.activity.ACT_UserTimeCard;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;
import com.yonyou.framework.library.common.utils.ToastUtil;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	private AppSharedPreferences orderSp;
	private String orderCode;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.pay_result);
		orderSp = new AppSharedPreferences(this);
//		orderCode = String.valueOf(orderSp.getSharedPreference(SpUtils.ORDER_CODE,""));
    	api = WXAPIFactory.createWXAPI(this, Constants.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
		Log.e("WX", "ddd");
	}

	@Override
	public void onResp(BaseResp resp) {
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			int code = resp.errCode;
			switch (code) {
				case 0:
					if (Constants.WX_PAY_TYPE == Constants.WX_PAY_RECHARGE){

					}else if (Constants.WX_PAY_TYPE == Constants.WX_PAY_CARD){
						ToastUtil.makeText(this,  "支付成功", false);
						Intent intent = new Intent(WXPayEntryActivity.this, ACT_UserTimeCard.class);
						startActivity(intent);
					}
					finish();
					break;
				case -1:
					ToastUtil.makeText(this,  "支付失败", false);
					finish();
					break;
				case -2:
					ToastUtil.makeText(this,  "支付取消, 请重新支付", false);
					finish();
					break;
				default:
					ToastUtil.makeText(this,  "支付失败, 请重新支付", false);
					setResult(RESULT_OK);
					finish();
					break;
			}

		}
	}

}